package com.example.wac.config;

import com.baidu.disconf.client.DisconfMgrBean;
import com.baidu.disconf.client.DisconfMgrBeanSecond;
import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuancheng
 * @description
 * @date 2022-01-04 14:09:00
 */

@Configuration
public class DisconfConfig {

    /**
     * 扫描@DisconfFile注解并注入值
     *
     * @return
     */
    @Bean(destroyMethod = "destroy")
    public DisconfMgrBean getDisconfMgrBean(){
        DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
        disconfMgrBean.setScanPackage("com.example.wac");
        return disconfMgrBean;
    }

    @Bean(destroyMethod = "destroy" , initMethod = "init")
    public DisconfMgrBeanSecond getDisconfMgrBeanSecond(){
        return new DisconfMgrBeanSecond();
    }

    /**
     * 将远程配置文件存入propertiesFactoryBean，propertiesFactoryBean再获取配置文件
     * @return
     */
    @Bean(name = "reloadablePropertiesFactoryBean")
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
    public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() {
        ReloadablePropertiesFactoryBean propertiesFactoryBean = new ReloadablePropertiesFactoryBean();
        propertiesFactoryBean.setSingleton(true);
        // 配置的文件
        List<String> fileNames = new ArrayList<>();
        fileNames.add("stockJob.properties");
        propertiesFactoryBean.setLocations(fileNames);
        return propertiesFactoryBean;
    }
}
